<template>
  <el-form size="mini">
    <el-form-item label="旧密码">
      <el-input v-model="formData.oldPassword"></el-input>
    </el-form-item>

    <el-form-item label="新密码">
      <el-input v-model="formData.newPassword"></el-input>
    </el-form-item>

    <el-form-item label="重复新密码">
      <el-input v-model="formData.newPassword2"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="changePasswrod">修改密码</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import {removeToken} from '@/utils/auth'
  export default {
    name: 'ProfileChangePassword',
    data: function() {
      return {
        formData: {
          oldPassword: null,
          newPassword: null,
          newPassword2: null
        }
      }
    },
    methods: {
      changePasswrod() {
        if (!this.formData.newPassword2 === this.formData.newPassword) {
          this.$message.error("新密码必须相同");
          return;
        }
        let formData_ = {...this.formData};
        delete formData_['newPassword2'];
        this.$confirm('确定修改密码吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.changePassword(formData_).then(res=>{
            Object.assign(this.$data.formData, this.$options.data().formData)
            removeToken()
            this.$router.replace("/");
          });
        })
      }
    }
  }
</script>

<style scoped>

</style>
